//
//  ZDSearchController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/14.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDSearchController.h"
#import "ZDSearchField.h"
#import "ZDGoodsListCell.h"
#import "ZDGoodsDetailModel.h"

#import "ZDGoodsDetialController.h"

static NSString *const ZDGoodsListCellId = @"ZDGoodsListCell";

@interface ZDSearchController ()
/**
 <#Description#>
 */
@property(nonatomic,strong)ZDSearchField *searchView;
@end

@implementation ZDSearchController

- (void)setupUI
{
    [super setupUI];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchClick)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    ZDSearchField *searchView = (ZDSearchField *) [UIView getNibView:@"ZDSearchField"];
    searchView.frame = CGRectMake(80, 5, LEScreenWidth - 155, 30);
    [self.navigationController.navigationBar addSubview:searchView];
    self.searchView = searchView;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZDGoodsListCell" bundle:nil] forCellWithReuseIdentifier:ZDGoodsListCellId];
    [self initHeader];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.searchView removeFromSuperview];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"1" forKey:@"flag"];
    [dict setObject:self.searchView.inputField.text forKey:@"keyword"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:MainSearchListUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        NSString *totalPage = successCode[@"page_total"];
        weakSelf.totalPage = totalPage;
        if (error)
        {
            [self addNoDataViewWithTipText:error];
            [weakSelf.dataSource removeAllObjects];
        }else
        {
            [self hiddenNoDataView];
            
            NSArray *tempt = datas[@"goods_list"];
            NSArray *tempModel = [ZDGoodsDetailModel mj_objectArrayWithKeyValuesArray:tempt];

            [weakSelf.dataSource removeAllObjects];
            [weakSelf.dataSource addObjectsFromArray:tempModel];
            
        }
        [weakSelf.collectionView reloadData];
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}
#pragma mark- collectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataSource.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((LEScreenWidth -24)/2, 200);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 8;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 8, 0, 8);
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZDGoodsListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ZDGoodsListCellId forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return  cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZDGoodsDetailModel *goods = self.dataSource[indexPath.row];
    ZDGoodsDetialController *goodsDetial = [[ZDGoodsDetialController alloc] init];
    goodsDetial.goodsId = goods.goods_id;
    goodsDetial.goodsName = goods.goods_name;
    [self.navigationController pushViewController:goodsDetial animated:YES];
}

#pragma mark- 点击事件
- (void)searchClick
{
    ZDLog(@"%s---%d---点击搜索",__func__,__LINE__);
    if (self.searchView.inputField.text.length == 0)
    {
        [SVProgressHUD showInfoWithStatus:@"请输入内容"];
        return;
    }
    [self.searchView.inputField resignFirstResponder];
    [self requestRefreshData];

}
@end












